package com.chengjue.transaction.learn.thread;

/**
 * 描述:
 *
 * @author Kipeng Huang
 * @create 2018-03-03 下午6:18
 */
public class TestSync implements Runnable {
	int b = 100;

	synchronized void m1() throws InterruptedException {
		b = 1000;
		System.out.println(b);
	}

	synchronized void m2() throws InterruptedException {
		b = 2000;
	}

	@Override
	public void run() {
		try {
			m1();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		TestSync testSync = new TestSync();

		/**
		 * 线程new状态
		 */
		Thread thread = new Thread(testSync);
		/**
		 * 线程runnable状态
		 * runnable状态表示可以执行，一切准备就绪，但并不是表示一定在CPU上执行，有没有真正执行取决于CPU的调度，之后才会调用run方法执行m1方法
		 */
		thread.start();
		testSync.m2();

		/**
		 * 如果testSync.m2()先获取锁 main thread b =2000  m1 b= 1000
		 * 如果thread先获取锁 b=1000  如果testSync.m2()还没来得及执行main b =1000 否则main b=2000
		 */
		System.out.println("main thread b=" + testSync.b);

	}
}
